package com.wondersgroup.esclient.service.impl;

import java.util.List;

import com.wondersgroup.esclient.emen.HttpMethod;
import com.wondersgroup.esclient.feign.FeignUitls;
import com.wondersgroup.esclient.http.HttpClientUtils;
import com.wondersgroup.esclient.modle.QueryParam;
import com.wondersgroup.esclient.service.BaseESService;
import com.wondersgroup.esclient.service.ESOriginalDataService;
import com.wondersgroup.esclient.util.ESDataUtil;

public class ESOriginalDataServiceImpl extends BaseESService implements ESOriginalDataService {

	public ESOriginalDataServiceImpl() {
		super();
	}

	public ESOriginalDataServiceImpl(Class<?> clazz, String clusterNode) {
		super(null, clusterNode, clazz.getName().toLowerCase(), clazz.getName());
	}

	public ESOriginalDataServiceImpl(String _index, String _type, String clusterNode) {
		super(null, clusterNode, _index, _type);
	}

	public String find(List<QueryParam> queryParams) throws Exception {
		String queryParam = ESDataUtil.getQueryParam(queryParams);
		String url = super.getESHost(true);
		return FeignUitls.httpPost(url + "/_search", queryParam);
	}

	public String findByPage(List<QueryParam> queryParams, int from, int size) throws Exception {
		String queryParam = ESDataUtil.getQueryParam(queryParams, from, size);
		String url = super.getESHost(true);
		return FeignUitls.httpPost(url + "/_search", queryParam);
	}

	public String sendHttpReqest(String url, String params, HttpMethod httpMethod) throws Exception {
		String returnData = null;
		switch (httpMethod) {
		case GET:
			returnData = HttpClientUtils.getInstance().sendHttpGet(url);
			break;
		case PUT:
			returnData = HttpClientUtils.getInstance().sendHttpPut(url, params);
			break;
		case POST:
			returnData = HttpClientUtils.getInstance().sendHttpPost(url, params);
			break;
		case DELETE:
			returnData = HttpClientUtils.getInstance().sendHttpDelete(url);
			break;
		}
		return returnData;
	}
}
